package com.jhd.fuzhou.view.daily;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.alibaba.fastjson.JSON;
import com.jhd.fuzhou.view.login.BaseActivity;
import com.jhd.fuzhou.R;
import com.jhd.fuzhou.service.SupervisionService;
import com.jhd.fuzhou.util.CommonQuestion;
import com.jhd.fuzhou.util.CustomToast;
import com.jhd.fuzhou.util.TextDialog;
import com.jhd.fuzhou.util.UtilHelper;
import com.jhd.fuzhou.vo.EnquiryQuestionBank;
import com.jhd.fuzhou.vo.JsonForResult;
import com.jhd.fuzhou.vo.QA;
import net.zigin.commons.util.StringUtil;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import static com.jhd.fuzhou.util.TextShow.highlight;
import static com.jhd.fuzhou.view.login.MainActivity.qaList;
/*询问笔录添加问题页面*/
public class DailyAskingQuestionActivity extends BaseActivity {
    @BindView(R.id.iv_cancel_question)ImageView iv_cancel_question;
    @BindView(R.id.iv_cancel_answer)ImageView iv_cancel_answer;
    @BindView(R.id.et_question)EditText et_question;
    @BindView(R.id.et_answer)EditText et_answer;
    @BindView(R.id.recyclerView)RecyclerView recyclerView;
    @BindView(R.id.btn_finish)Button btn_finish;
    @BindView(R.id.tv_common_question)TextView tv_common_question;
    @BindView(R.id.lin_question)LinearLayout lin_question;
    private Context context= DailyAskingQuestionActivity.this;
    private LinearLayoutManager layoutManager;
    private String chooseString="";
    private List<String> commonQuestion;
    private List<String> searchQuestion=new ArrayList<>();
    private AskingContentAdapter askingContentAdapter;


    @Override
    protected void loadViewLayout() {
        setContentView(R.layout.activity_asking_question);
    }

    @Override
    protected void processLogic() {
        commonQuestion= CommonQuestion.addQuestion();
        layoutManager = new LinearLayoutManager(context);
        recyclerView.setLayoutManager(layoutManager);
        askingContentAdapter=new AskingContentAdapter(commonQuestion);
        recyclerView.setAdapter(askingContentAdapter);
        findViewById(R.id.iv_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onBackPressed();
            }
        });
        iv_cancel_question.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                iv_cancel_question.setVisibility(View.GONE);
                et_question.setText("");
                tv_common_question.setVisibility(View.VISIBLE);
            }
        });
        iv_cancel_answer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                iv_cancel_answer.setVisibility(View.GONE);
                et_answer.setText("");
            }
        });
        et_question.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                getQuestion();
                return false;
            }
        });
        et_question.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) { }
            @Override
            public void afterTextChanged(Editable s) {
                if((!et_question.getText().toString().equals(chooseString))&&(!chooseString.equals(""))){
                    lin_question.setVisibility(View.VISIBLE);
                }
                if(et_question.getText().toString().equals("")){
                    askingContentAdapter=new AskingContentAdapter(commonQuestion);
                    recyclerView.setAdapter(askingContentAdapter);
                    tv_common_question.setVisibility(View.VISIBLE);
                }else{
                    if((!et_question.getText().toString().equals(chooseString))&&(!chooseString.equals(""))){
                        lin_question.setVisibility(View.VISIBLE);
                    }
                }
                if(s.toString().equals("")){
                    getQuestion();
                    iv_cancel_question.setVisibility(View.GONE);
                    tv_common_question.setVisibility(View.VISIBLE);
                }else{
                    iv_cancel_question.setVisibility(View.VISIBLE);
                    tv_common_question.setVisibility(View.GONE);
                }
            }
        });
        et_answer.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if(s.toString().equals("")){
                    iv_cancel_answer.setVisibility(View.GONE);
                }else{
                    iv_cancel_answer.setVisibility(View.VISIBLE);
                }
            }
        });
    }

    private void getQuestion(){
        Retrofit retrofit = UtilHelper.retrofit(application, UtilHelper.baseUrls.API());//URL的头
        SupervisionService vs = retrofit.create(SupervisionService.class);
        Map<String, String> params = new LinkedHashMap<String, String>();
        params.put("keyword",et_question.getText().toString());//模糊查询
        Call<JsonForResult> jsonForResultCall = vs.getEnquiryQuestionBankList(application.getAccessToken(), params);//调用接口获取json返回
        jsonForResultCall.enqueue(new Callback<JsonForResult>() {
            @Override
            public void onResponse(Call<JsonForResult> call, Response<JsonForResult> response) {
                try {
                    JsonForResult jsonForResult = (JsonForResult) response.body();
                    if (jsonForResult != null && jsonForResult.isSuccess()) {
                        searchQuestion.clear();
                        if (jsonForResult.getData() != null) {
                            List<EnquiryQuestionBank> enquiryQuestionBankList = JSON.parseArray(jsonForResult.getData().toString(), EnquiryQuestionBank.class);
                            for (EnquiryQuestionBank enquiryQuestionBank : enquiryQuestionBankList) {
                                searchQuestion.add(enquiryQuestionBank.getQuestion());
                            }
                        }
                        askingContentAdapter=new AskingContentAdapter(searchQuestion);
                        recyclerView.setAdapter(askingContentAdapter);
                        if(searchQuestion.size()==0){
                            lin_question.setVisibility(View.GONE);
                        }else{
                            lin_question.setVisibility(View.VISIBLE);
                        }

                    }
                } catch (Exception e) {
//                    CustomToast.showText(context,e.getLocalizedMessage(),3000,0,0).show();
                }
            }

            @Override
            public void onFailure(Call<JsonForResult> call, Throwable t) {
                String msg = "网络异常!";
                if (StringUtil.isNullOrEmpty(t.getMessage()) == false) msg = msg;
                UtilHelper.alertMsg(context, "错误", msg);
            }
        });
    }

    @OnClick({R.id.iv_cancel_question,R.id.iv_cancel_answer,R.id.btn_finish,R.id.et_question,R.id.et_answer})
    public void onClick(View v){
        switch (v.getId()){
            case R.id.et_question:
                TextDialog.showDialog(context,"输入问题",et_question);
                break;
            case R.id.et_answer:
                TextDialog.showDialog(context,"输入答案",et_answer);
                break;
            case R.id.iv_cancel_question://该处需要对常用问题刷新
                et_question.setText("");
                break;
            case R.id.iv_cancel_answer:
                et_answer.setText("");
                break;
            case R.id.btn_finish://
                QA qa=new QA();
                if(et_answer.getText().toString().trim().equals("")||et_question.getText().toString().trim().equals("")){
                    CustomToast.showText(context,"问题和答案不能为空！",3000,0,0).show();
                }else{
                    qa.setAnswer(et_answer.getText().toString());
                    qa.setQuestion(et_question.getText().toString());
                    qaList.add(qa);
                    finish();
                }
                break;
        }
    }

    public class AskingContentAdapter extends RecyclerView.Adapter<AskingContentAdapter.ViewHolder> {
        private List<String> question;

        public AskingContentAdapter(List<String> question) {
            this.question = question;
        }

        @NonNull
        @Override
        public AskingContentAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_question, parent, false);
            AskingContentAdapter.ViewHolder viewHolder = new AskingContentAdapter.ViewHolder(itemView);
            return viewHolder;
        }

        @Override
        public void onBindViewHolder(@NonNull AskingContentAdapter.ViewHolder holder, int position) {
            holder.tv_question.setText(highlight(question.get(position),et_question.getText().toString()));
            holder.tv_question.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    et_question.setText(question.get(position));
                    lin_question.setVisibility(View.GONE);//暂时将recyclerView隐藏
                    chooseString=question.get(position);
                }
            });


        }

        @Override
        public int getItemCount() {
            return question.size();
        }

        class ViewHolder extends RecyclerView.ViewHolder {
            TextView tv_question;
            public ViewHolder(View itemView) {
                super(itemView);
                this.tv_question = itemView.findViewById(R.id.tv_question);
            }
        }
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}